package com.yongninger.activity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.yongninger.dao.ShipmentsDao;
import com.yongninger.vo.ClerkInfo;
import com.yongninger.vo.CustomerInfo;
import com.yongninger.vo.ShipmentsInfoVo;
import com.yongninger.vo.ShipmentsVo;

public class ReportActivity extends Activity {
	private ShipmentsDao shipmentsDao;
	private String[] arrClerk;
	CustomerInfo[] arrCustomer;
	ShipmentsInfoVo[] arrShipmentsInfoVo;
	private EditText showDate2 = null;
	private EditText showDate3 = null;
	private static final int DATE_DIALOG_ID = 1;
	private static final int SHOW_DATAPICK = 0;
	private int mYear;
	private int mMonth;
	private int mDay;
	private int dateTemp = 2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.report);
		/**
		 * 开始日期
		 */
		showDate2 = (EditText) findViewById(R.id.sdate2);
		showDate2.setOnClickListener(new DateButtonOnClickListener());
		final Calendar c2 = Calendar.getInstance();
		mYear = c2.get(Calendar.YEAR);
		mMonth = c2.get(Calendar.MONTH);
		mDay = c2.get(Calendar.DAY_OF_MONTH);
		//setDateTime();
		/**
		 * 结束日期
		 */
		showDate3 = (EditText) findViewById(R.id.sdate3);
		showDate3.setOnClickListener(new DateButtonOnClickListener());
		final Calendar c3 = Calendar.getInstance();
		mYear = c3.get(Calendar.YEAR);
		mMonth = c3.get(Calendar.MONTH);
		mDay = c3.get(Calendar.DAY_OF_MONTH);
		//setDateTime();
		/*
		 * 选择会员区域
		 */
		
		shipmentsDao = new ShipmentsDao(getApplicationContext());
		List<ClerkInfo> listClerk = shipmentsDao.queryClerkInfoVo();
		arrClerk = new String[listClerk.size()+1];
		arrClerk[0] = "请选择";
		for (int i = 0; i < listClerk.size(); i++) {
			arrClerk[i+1] = listClerk.get(i).getCname();
		}
		Spinner spinner = (Spinner) findViewById(R.id.sclerk);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, arrClerk);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
		spinner.setVisibility(View.VISIBLE);
		/*
		 * 发货组织选择
		 */
		List<ShipmentsInfoVo> shipmentsInfoVos = shipmentsDao
				.queryShipmentsInfoVo();
		String[] shipInfoArr = new String[shipmentsInfoVos.size()+1];
		arrShipmentsInfoVo = new ShipmentsInfoVo[shipmentsInfoVos.size()+1];
		shipInfoArr[0] = "请选择";
		arrShipmentsInfoVo[0] = null;
		for (int i = 0; i < shipmentsInfoVos.size(); i++) {
			shipInfoArr[i+1] = shipmentsInfoVos.get(i).getSname();
			arrShipmentsInfoVo[i+1] = shipmentsInfoVos.get(i);
		}
		Spinner spinnerShipInfo = (Spinner) findViewById(R.id.scompany);
		ArrayAdapter<String> adapterShipInfo = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, shipInfoArr);
		adapterShipInfo
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerShipInfo.setAdapter(adapterShipInfo);
		spinnerShipInfo.setVisibility(View.VISIBLE);
		// 按钮点击事件
		Button addBtn = (Button) this.findViewById(R.id.query);
		OnClickListener onClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//根据条件查询数据库，显示到页面
				query();
			}
		};
		addBtn.setOnClickListener(onClickListener);
	}
	/**
	 * 
	 * @author alex.duan 通过会员选择 得到客户信息
	 */
	class SpinnerSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			List<CustomerInfo> listCustomer = shipmentsDao
					.queryCustomerInfoVo(arrClerk[arg2]);
			arrCustomer = new CustomerInfo[listCustomer.size()+1];
			String[] str = new String[listCustomer.size()+1];
			str[0] = "请选择";
			arrCustomer[0] = null;
			for (int i = 0; i < listCustomer.size(); i++) {
				arrCustomer[i+1] = listCustomer.get(i);
				str[i+1] = listCustomer.get(i).getCname();
			}
			// 点击会员选择
			Spinner spinner = (Spinner) findViewById(R.id.scustomer);
			// 将可选内容与ArrayAdapter连接起来
			ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
					ReportActivity.this, android.R.layout.simple_spinner_item, str);
			// 设置下拉列表的风格
			adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			// 将adapter 添加到spinner中
			spinner.setAdapter(adapter1);
			// 设置默认值
			spinner.setVisibility(View.VISIBLE);
		}

		public void onNothingSelected(AdapterView<?> arg0) {

		}

	}
	
	private void query(){
		//查询参数得到值
		Spinner spinner = (Spinner) findViewById(R.id.scustomer);
		String scustomer = spinner.getSelectedItem().toString();
		Spinner spinnerShipInfo = (Spinner) findViewById(R.id.scompany);
		String scompany = spinnerShipInfo.getSelectedItem().toString();
		Spinner spinner1 = (Spinner) findViewById(R.id.sclerk);
		String sclerk = spinner1.getSelectedItem().toString();
		
		ShipmentsVo shipmentsVo = new ShipmentsVo();
		shipmentsVo.setScompany(scompany);
		shipmentsVo.setSdate2(showDate2.getText().toString());
		shipmentsVo.setSdate3(showDate3.getText().toString());
		shipmentsVo.setSclerk(sclerk);
		shipmentsVo.setScustomer1(scustomer);
		//查询出来数据
		ListView listView = (ListView) this.findViewById(R.id.listView);
		ShipmentsDao shipmentsDao = new ShipmentsDao(getApplicationContext());
		List<ShipmentsVo> listShipmentsVos = shipmentsDao
				.queryShipmentsVoList(shipmentsVo,0,0);
		//显示到页面
		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		double countall = 0D;
		int sum1all = 0;
		int sum2all = 0;
		for (ShipmentsVo vo : listShipmentsVos) {
			HashMap<String, Object> item = new HashMap<String, Object>();
			item.put("scode", vo.getScode());
			item.put("sclerk", vo.getSclerk());
			item.put("scustomer", vo.getScustomer1());
			item.put("saddress", vo.getSaddress());
			item.put("sperson", vo.getSperson());
			item.put("scontact", vo.getScontact());
			item.put("scontent", vo.getScontent());
			item.put("sproductname", vo.getSproductname());
			item.put("sproducttype", vo.getSproducttype());
			item.put("sproductsum1", vo.getSproductsum1());
			item.put("sproductsum2", vo.getSproductsum2());
			item.put("sproductprice", vo.getSproductprice());
			item.put("sproductcount", vo.getSproductcount());
			data.add(item);
			//统计
			countall += Double.parseDouble(vo.getSproductcount());
			String sum1 = vo.getSproductsum1();
			String sum2 = vo.getSproductsum2();
			sum1all += Double.parseDouble(sum1.substring(0, sum1.length()-1));
			sum2all += Double.parseDouble(sum2.substring(0, sum2.length()-1));
		}
		// 创建SimpleAdapter适配器将数据绑定到item显示控件上
		SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.ships_item,
				new String[] { "sproductname", "sproductsum1", "sproductsum2",
						"sproductprice", "sproductcount", "sclerk" },
				new int[] { R.id.sproductname, R.id.sproductsum1,
						R.id.sproductsum2, R.id.sproductprice,
						R.id.sproductcount, R.id.sclerk });
		// 实现列表的显示
		listView.setAdapter(adapter);
		//统计显示
		DecimalFormat df = new DecimalFormat("#.##");
		TextView countallTv = (TextView)findViewById(R.id.sproductcountall);
		countallTv.setText(df.format(countall));
		TextView sum1allTv = (TextView)findViewById(R.id.sproductsum1all);
		sum1allTv.setText(String.valueOf(sum1all));
		TextView sum2allTv = (TextView)findViewById(R.id.sproductsum2all);
		sum2allTv.setText(String.valueOf(sum2all));
	}
	private void setDateTime() {

		final Calendar c = Calendar.getInstance();

		mYear = c.get(Calendar.YEAR);

		mMonth = c.get(Calendar.MONTH);

		mDay = c.get(Calendar.DAY_OF_MONTH);

		updateDisplay();

	}

	/**
	 * 
	 * 更新日期
	 */

	private void updateDisplay() {
		if(dateTemp == 2){
			showDate2.setText(new StringBuilder().append(mYear).append(

					(mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)).append(

					(mDay < 10) ? "0" + mDay : mDay));
		}else if(dateTemp == 3){
			showDate3.setText(new StringBuilder().append(mYear).append(

					(mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)).append(

					(mDay < 10) ? "0" + mDay : mDay));
		}

	}

	/**
	 * 
	 * 日期控件的事件
	 */

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,

		int dayOfMonth) {

			mYear = year;

			mMonth = monthOfYear;

			mDay = dayOfMonth;

			updateDisplay();

		}

	};

	/**
	 * 
	 * 选择日期Button的事件处理
	 * 
	 * 
	 * 
	 * @author Raul
	 * 
	 * 
	 */

	class DateButtonOnClickListener implements

	android.view.View.OnClickListener {

		@Override
		public void onClick(View v) {
			Message msg = new Message();
			if (showDate2.equals((EditText) v)) {
				dateTemp = 2;
				msg.what = ReportActivity.SHOW_DATAPICK;
			}
			if (showDate3.equals((EditText) v)) {
				dateTemp = 3;
				msg.what = ReportActivity.SHOW_DATAPICK;
			}
			ReportActivity.this.saleHandler.sendMessage(msg);
		}

	}

	@Override
	protected Dialog onCreateDialog(int id) {

		switch (id) {

		case DATE_DIALOG_ID:

			return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,

			mDay);

		}

		return null;

	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {

		switch (id) {

		case DATE_DIALOG_ID:

			((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);

			break;

		}

	}

	/**
	 * 
	 * 处理日期控件的Handler
	 */

	Handler saleHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {

			case ReportActivity.SHOW_DATAPICK:

				showDialog(DATE_DIALOG_ID);

				break;

			}

		}

	};

}
